﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.IO.Ports;
using System.IO;

namespace IDCardReader3in1
{
    public partial class FormSetting : MetroForm
    {
        private IDCReaderOption _option = new IDCReaderOption();
        public IDCReaderOption Option
        {
            get { return _option; }
            set { _option = value; }
        }

        public FormSetting()
        {            
            InitializeComponent();
            LoadConfig();
        }

        private void LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }


        private void InitCtrls()
        {
            metroComboBoxReaderName.Items.Clear();
            string[] names = MrtdSDKNet40.ReadMrtdDocApi.Instance.GetReaderList();
            if (names != null)
            {
                metroComboBoxReaderName.Items.AddRange(names);
                for (int i = 0; i < metroComboBoxEncoding.Items.Count; i++)
                {
                    if (_option.EncodingName.Equals(metroComboBoxEncoding.Items[i].ToString()))
                    {
                        metroComboBoxEncoding.SelectedIndex = i;
                    }
                }
            }
            else
            {
                MessageBox.Show("未识别到读写器");
            }

            metroToggleReadHeadImage.Checked = _option.ReadHeadImage;
            metroToggleReadFingerprint.Checked = _option.ReadFingerprint;
            intRangeTrackBarScanInterval.Value = _option.ScanInterval;
            metroComboBoxReaderName.Text = _option.ReaderName;
            //string defaultComNo = "COM" + _option.ReaderName.ToString();
            //for (int i = 0; i < metroComboBoxReaderName.Items.Count; i++)
            //{
            //    if (defaultComNo.Equals(metroComboBoxReaderName.Items[i].ToString()))
            //    {
            //        metroComboBoxReaderName.SelectedIndex = i;
            //    }
            //}
        }

        private bool UpdateData()
        {
            if(metroComboBoxReaderName.SelectedItem == null)
            {
                MessageBox.Show("没有选择任何一个读写器");
                return false;
            }
            string readerName = metroComboBoxReaderName.SelectedItem.ToString();          
       
            _option.ReaderName = readerName;
            _option.EncodingName = metroComboBoxEncoding.SelectedItem.ToString();
            _option.ReadHeadImage = metroToggleReadHeadImage.Checked;
            _option.ReadFingerprint = metroToggleReadFingerprint.Checked;
            _option.ScanInterval = intRangeTrackBarScanInterval.Value;
            return _option.Save();
        }

    
        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (UpdateData())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }           
        }

        private void FormIDCReaderSetting_Load(object sender, EventArgs e)
        {
            InitCtrls();
        }
    }
}
